import React,{useState,useEffect } from 'react'
import reactDom from 'react-dom';

export default function CountSet(props){
    let [count ,setCount] = useState(5) 

    useEffect(() => {
        let timer = setInterval(()=>{
            setCount(count--)
        },1000)

        if(count <= 0){
            clearInterval(timer)
        }

        return ()=>clearInterval(timer)
    }, [count]); 

    function unmount (){
        reactDom.unmountComponentAtNode(document.getElementById('root'))
    }
    return (
        <>
            当前count是 {count}
            <br />
            <button onClick={() => setCount(count + 1)}>
                Click me  + 1
            </button>
            <button onClick={unmount}>卸載組件</button>
        </>
    )
}